package nix.anim.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import nix.anim.common.pojo.LogAnnotation;
import nix.anim.common.pojo.NixResult;
import nix.anim.pojo.User;
import nix.anim.service.UserService;

/**
 * 用户管理Controller
 */
@Controller
public class UserController {

	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/api/user/list", method=RequestMethod.GET)
	@ResponseBody
	public NixResult getUserList(Integer page, Integer rows) {		
		// 调用服务
		NixResult result = userService.getUserList(page, rows);
		return result;
	}
	
	@RequestMapping(value="/api/user/save", method=RequestMethod.POST)
	@ResponseBody
	@LogAnnotation(action="新增/修改", object="用户")
	public NixResult saveUser(User user) {
		NixResult result = new NixResult();
		if (user.getId() == null) {
			// Add
			result = userService.addUser(user);
		} else {
			// Update
			result = userService.updateUser(user);
		}
		return result;
	}
	
	@RequestMapping(value="/api/user/resetpwd/{id}", method=RequestMethod.POST)
	@ResponseBody
	@LogAnnotation(action="重置密码", object="用户")
	public NixResult resetPassword(@PathVariable Integer id) {
		// 调用服务
		NixResult result = userService.resetPassword(id);
		return result;
	}
	
	@RequestMapping(value="/api/user/role/{id}", method=RequestMethod.GET)
	@ResponseBody
	public NixResult getUserRoleIds(@PathVariable Integer id) {
		// 调用服务
		NixResult result = userService.getUserRoleIds(id);
		return result;
	}
	
	@RequestMapping(value="/api/user/setrole", method=RequestMethod.POST)
	@ResponseBody
	@LogAnnotation(action="指定角色", object="用户角色")
	public NixResult setUserRole(Integer userId, String roleIds) {
		// 调用服务
		NixResult result = userService.setUserRole(userId, roleIds);
		return result;
	}
}
